﻿using StockTrading.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static StockTrading.Model.Order;

namespace StockTrading.ViewModel
{
    public class StockDetailsViewModel:NotificationBase
    {
        private readonly ApplicationDbContext context;
        private StockInfo _StockInfo;

        public StockInfo StockInfo
        {
            get { return _StockInfo; }
            set
            {
                this._StockInfo = value;
                this.RaisePropertyChange("StockInfo");
            }
        }

        private Stock _CurrentStock;
        public double Benefit { get; } = new Random().NextDouble() * 200;
        public CommandBase SellCommand { get; set; }
        public CommandBase BuyCommand { get; set; }
        private OperationModel _op;
        private bool IsProcess = false;
        public OperationModel op
        {
            get { return _op; }
            set
            {
                this._op = value;
                this.RaisePropertyChange("op");
            }
        }

        public Stock CurrentStock
        {
            get { return _CurrentStock; }
            set
            {
                this._CurrentStock = value;
                this.RaisePropertyChange("CurrentStock");
            }
        }

        private LoginViewModel _LoginUser;
        public LoginViewModel LoginUser
        {
            get { return _LoginUser; }
            set
            {
                if (value == null) return;
                this._LoginUser = value;
                this.RaisePropertyChange("LoginUser");
            }
        }

        public StockDetailsViewModel(ApplicationDbContext context,LoginViewModel user)
        {
            this.context = context;
            _LoginUser = user;
            this.StockInfo = new StockInfo();
            this.op = new OperationModel();
            this.BuyCommand = new CommandBase();
            this.BuyCommand.ExecuteAction = BuyStock;
            this.BuyCommand.CanExecuteFunc = CanExecute;
            this.SellCommand = new CommandBase();
            this.SellCommand.ExecuteAction = SellStock;
            this.SellCommand.CanExecuteFunc = CanExecute;
        }
        private bool CanExecute(object p)
        {
                if (p == null&&!IsProcess) return true;
                else return false;
        }
        private async void BuyStock(object param)
        {
            await StockOpertion(OrderType.Buy);
        }
        private async void SellStock(object param)
        {
            
            await StockOpertion(OrderType.Sell);
            
        }
        private async Task StockOpertion(OrderType orderType)
        {
            IsProcess = true;
            await context.Orders.AddAsync(new Order()
            {
                UserId = this.LoginUser.UserModel.Id,
                StockId = this.CurrentStock.Id,
                CreateDate = System.DateTime.Now,
                Type = orderType,
                Price = op.Price,
                Undealed = op.Num,
                Dealed = 0,
                Canceled = 0
            });
            await context.SaveChangesAsync();
            IsProcess = false;
        }
    }
}
